/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageBean;

import HouseBN.HouseService;
import LinkImageBN.InsertimginDB;
import LinkImageBN.LinkimageService;
import entity.House;
import entity.LinkImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author Mr-Quick
 */
@ManagedBean
@SessionScoped
public class Editmessage {

    public Editmessage() {
        photos = new ArrayList<LinkImage>();
    }
    String idhouse;
    House house;
    int idphoto;
    List<LinkImage> listphoto;
    List<LinkImage> photos;

    public List<LinkImage> getPhotos() {
        return photos;
    }

    public List<LinkImage> getListphoto() {
        return listphoto;
    }

    public int getIdphoto() {
        return idphoto;
    }

    public void setIdphoto(int idphoto) {
        this.idphoto = idphoto;
    }

    public String getIdhouse() {
        return idhouse;
    }


    public House getHouse() {
        return house;
    }

    public void setHouse(House house) {
        this.house = house;
    }

    public void setIdhouse(String idhouse) {
        this.idhouse = idhouse;
        Getdetailmessage();
        Getlinkimagefordetailmassage();
    }

    public void Getdetailmessage() {
        HouseService hs = new HouseService();
        setHouse(hs.Gethousebyid(idhouse));
    }

    public void Getlinkimagefordetailmassage() {
        LinkimageService lis = new LinkimageService();
        listphoto = lis.Getlinkimagebyidhouse(idhouse);
    }

    public void handleFileUpload(FileUploadEvent event) {
        FileOutputStream outStream = null;
        try {
            ExternalContext exContext = FacesContext.getCurrentInstance().getExternalContext();
            String fileName = event.getFile().getFileName();
            String ext = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + ext;
            String realFolder = exContext.getRealPath("/images/");
            File file = new File(realFolder);
            if (!file.exists()) {
                file.mkdirs();
            }
            outStream = new FileOutputStream(realFolder + "/" + newFileName);
            outStream.write(event.getFile().getContents());
            entity.LinkImage photo = new entity.LinkImage();
            photo.setId(0);
            photo.setLinkImageHouse("/images/" + newFileName);
            photos.add(photo);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(UploadPicture.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(UploadPicture.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                outStream.close();
            } catch (IOException ex) {
                Logger.getLogger(UploadPicture.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public String Updatemessage() {
        try {
            HouseService hs = new HouseService();
            hs.Updatehouse(house, idhouse);
            InsertimginDB iimg = new InsertimginDB();
            iimg.insertimg(photos, idhouse);
            clear();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "Editmessage.xhtml?faces-redirect=true&id=" + idhouse;
    }

    public String Deletelinkimage() {
        LinkimageService lis = new LinkimageService();
        lis.deletelinkImageHouse(idphoto);
        return "Editmessage.xhtml?faces-redirect=true&id=" + idhouse;
    }

    private void clear() {
        photos = new ArrayList<LinkImage>();
        listphoto = new ArrayList<LinkImage>();
    }
}
